// pages/fill/volunteerList/volunteerList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    tabsindex: 0,
    volunteerList: [],
    ids: [],
    tabs: [{
        tips: '可冲击'
      },
      {
        tips: '较稳定'
      },
      {
        tips: '可保底'
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options--------', options);
    if (options.chooseList) {
      const majorList = JSON.parse(options.chooseList).majorList.map(item => {
        return item.id
      })
      const addressList = JSON.parse(options.chooseList).addressList.map(item => {
        return item.value
      })
      this.setData({
        majorList: majorList,
        addressList: addressList
      })
      this.getvolunteerList(majorList, addressList)
    }
    console.log('app.globalData.userInfo', app.globalData.user)
    const {
      score,
      ranking
    } = app.globalData.user
    this.setData({
      score,
      ranking
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabsClick(e) {
    this.setData({
      tabsindex: e.currentTarget.dataset.index
    })
  },
  // 可冲击切换
  switch1Change(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.switch = !item.switch
    item.isObey = item.switch ? 1 : 0
    this.setData({
      [`bluntSchoolList[${index}]`]: item
    })
  },
  // 较稳定切换
  switch2Change(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.switch = !item.switch
    item.isObey = item.switch ? 1 : 0
    this.setData({
      [`steadySchoolList[${index}]`]: item
    })
  },
  // 可保底切换
  switch3Change(e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    item.switch = !item.switch
    item.isObey = item.switch ? 1 : 0
    this.setData({
      [`keepSchoolList[${index}]`]: item
    })
  },
  getvolunteerList(majorList, addressList) {
    const userId = app.globalData.user.id
    // let userId = 1
    app.globalData.wxRequest.request('volunteer/getVolunteer.do', {
      majorList,
      addressList,
      userId: userId
    }).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      console.log("123", res.data);
      const {
        bluntSchoolList,
        keepSchoolList,
        steadySchoolList
      } = res.data
      bluntSchoolList.map(item => {
        item.switch = item.isObey == 1 ? true : false
      })
      keepSchoolList.map(item => {
        item.switch = item.isObey == 1 ? true : false
      })
      steadySchoolList.map(item => {
        item.switch = item.isObey == 1 ? true : false
      })
      this.setData({
        bluntSchoolList,
        keepSchoolList,
        steadySchoolList
      })
    })
  },
  hidecertificates() {
    const {
      bluntSchoolList,
      keepSchoolList,
      steadySchoolList
    } = this.data
    const userId = app.globalData.user.id
    app.globalData.wxRequest.request('volunteer/addVolunteer.do', {
      userId,
      bluntSchoolList,
      keepSchoolList,
      steadySchoolList
    }).then(res => {
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      if (res.code == 200) {
        wx.redirectTo({
          url: `/pages/fill/myvolunteerList/myvolunteerList?id=${res.data.id}`,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },
  totop(e) {
    let that = this;
    let sindex = e.currentTarget.dataset.sindex;
    let mindex = e.currentTarget.dataset.mindex;
    let tab = e.currentTarget.dataset.tab;
    let schoolList = [];
    let majorList = [];
    switch (tab) {
      case "0":
        schoolList = that.data.bluntSchoolList;
        majorList = [schoolList[sindex].volunteerMajorList[mindex]];
        schoolList[sindex].volunteerMajorList.splice(mindex, 1);
        schoolList[sindex].volunteerMajorList = majorList.concat(schoolList[sindex].volunteerMajorList);
        that.setData({
          bluntSchoolList: schoolList
        })
        console.error(that.data.bluntSchoolList)
        break;
      case "1":
        schoolList = that.data.steadySchoolList;
        majorList = [schoolList[sindex].volunteerMajorList[mindex]];
        schoolList[sindex].volunteerMajorList.splice(mindex, 1);
        schoolList[sindex].volunteerMajorList = majorList.concat(schoolList[sindex].volunteerMajorList);
        that.setData({
          steadySchoolList: schoolList
        })
        console.error(that.data.steadySchoolList)
        break;
      case "2":
        schoolList = that.data.keepSchoolList;
        majorList = [schoolList[sindex].volunteerMajorList[mindex]];
        schoolList[sindex].volunteerMajorList.splice(mindex, 1);
        schoolList[sindex].volunteerMajorList = majorList.concat(schoolList[sindex].volunteerMajorList);
        that.setData({
          keepSchoolList: schoolList
        })
        console.error(that.data.keepSchoolList)
        break;
    }
  },
  change(e) {
    let data = e.currentTarget.dataset.data;
    data.majorId = e.currentTarget.dataset.majorid;
    data.tab = e.currentTarget.dataset.tab;
    data.sindex = e.currentTarget.dataset.sindex;
    data.mindex = e.currentTarget.dataset.mindex;
    // console.error(data)
    let arr = JSON.stringify(data);

    wx.navigateTo({
      url: '/pages/fill/change/change?data=' + arr,
    })
  },
  changeMajor(data) {
    let that = this;
    let sindex = data.sindex;
    let mindex = data.mindex;
    let tab = data.tab;
    let schoolList = [];
    let value = data.value;
    switch (tab) {
      case "0":
        schoolList = that.data.bluntSchoolList;
        schoolList[sindex].volunteerMajorList[mindex] = value
        that.setData({
          bluntSchoolList: schoolList
        })
        break;
      case "1":
        console.error(that.data.steadySchoolList)
        schoolList = that.data.steadySchoolList;
        console.error(schoolList)
        schoolList[sindex].volunteerMajorList[mindex] = value
        console.error(schoolList);
        that.setData({
          steadySchoolList: schoolList
        })
        break;
      case "2":
        schoolList = that.data.keepSchoolList;
        schoolList[sindex].volunteerMajorList[mindex] = value
        that.setData({
          keepSchoolList: schoolList
        })
        break;
    }
  },
  changeSchool(data) {
    console.error(data)
    let that = this;
    let sindex = data.sindex;
    let tab = data.tab;
    let schoolList = [];
    let value = data.value;
    switch (tab) {
      case 0:
        schoolList = that.data.bluntSchoolList;
        schoolList[sindex] = value
        that.setData({
          bluntSchoolList: schoolList
        })
        break;
      case 1:
        schoolList = that.data.steadySchoolList;
        schoolList[sindex] = value
        that.setData({
          steadySchoolList: schoolList
        })
        break;
      case 2:
        schoolList = that.data.keepSchoolList;
        schoolList[sindex] = value
        that.setData({
          keepSchoolList: schoolList
        })
        break;
    }

  },
  toTopVolunteer(e) {
    console.log(e);
    let that = this;
    let tab = e.currentTarget.dataset.sindex;
    let mindex = e.currentTarget.dataset.index;
    let schoolList = [];
    let currentData
    switch (tab) {
      case 0:
        schoolList = JSON.parse(JSON.stringify(that.data.bluntSchoolList));
        currentData = JSON.parse(JSON.stringify(schoolList.splice(mindex, 1)))
        schoolList = currentData.concat(schoolList);
        that.setData({
          bluntSchoolList: schoolList
        })
        console.error(that.data.bluntSchoolList)
        break;
      case 1:
        schoolList = JSON.parse(JSON.stringify(that.data.steadySchoolList));
        currentData = JSON.parse(JSON.stringify(schoolList.splice(mindex, 1)))
        schoolList = currentData.concat(schoolList);
        that.setData({
          steadySchoolList: schoolList
        })
        console.error(that.data.steadySchoolList)
        break;
      case 2:
        schoolList = JSON.parse(JSON.stringify(that.data.keepSchoolList));
        currentData = JSON.parse(JSON.stringify(schoolList.splice(mindex, 1)))
        schoolList = currentData.concat(schoolList);
        that.setData({
          keepSchoolList: schoolList
        })
        console.error(that.data.keepSchoolList)
        break;
    }
    console.log('---------------');
  },
  changeVolunteer(e) {
    let that = this;
    let bluntSchoolList = that.data.bluntSchoolList;
    let steadySchoolList = that.data.steadySchoolList;
    let keepSchoolList = that.data.keepSchoolList;
    let list = bluntSchoolList.concat(steadySchoolList).concat(keepSchoolList);
    let data = {};
    data.tab = e.currentTarget.dataset.sindex;
    data.sindex = e.currentTarget.dataset.index;
    data.majorList = that.data.majorList;
    data.schoolList = list;
    data.score = that.data.score;
    let arr = JSON.stringify(data);
    wx.navigateTo({
      url: '/pages/fill/changeVolunteer/changeVolunteer?data=' + arr+'&&id='+e.currentTarget.id,
    })

  },
  deleteVolunteer(e) {
    let that = this;
    console.log(e);
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      confirmText: '确定',
      content: '您确定是否要删除该志愿？',
      showCancel: true,
      confirmColor: '#2B6FED',
      cancelColor: 'rgba(3,3,3,0.25)',
      success(res) {
        if (res.confirm) {
          console.log('确认');
          if (e.currentTarget.dataset.sindex == 0) {
            let bluntSchoolList = that.data.bluntSchoolList;
            bluntSchoolList.splice(index, 1);
            that.setData({
              bluntSchoolList
            })
          }
          if (e.currentTarget.dataset.sindex == 1) {
            let steadySchoolList = that.data.steadySchoolList;
            steadySchoolList.splice(index, 1);
            that.setData({
              steadySchoolList
            })
          }
          if (e.currentTarget.dataset.sindex == 2) {
            let keepSchoolList = that.data.keepSchoolList;
            keepSchoolList.splice(index, 1);
            that.setData({
              keepSchoolList
            })
          }
        }
        if (res.cancel) {
          console.log('取消');
          return
        }
      }
    })
  },
})